package com.zeei.ems.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SmUtil;
import com.github.pagehelper.PageHelper;
import com.zeei.ems.base.dao.UserDao;
import com.zeei.ems.base.service.UserService;
import com.zeei.ems.base.util.BCryptUtil;
import com.zeei.ems.base.vo.UserParam;
import com.zeei.ems.base.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    UserDao userDao;

    @Override
    public List<UserVO> listUser(Integer pageSize, Integer pageNum, UserParam userParam) {
        long total = 0;
        if (pageSize != null && pageNum != null) {
            PageHelper.startPage(pageNum, pageSize);
        }

        return userDao.listUser(userParam);
    }

    @Override
    @Transactional
    public Boolean addUser(UserVO userVo) {
        addUserToDb(userVo);
        return Boolean.TRUE;
    }

    @Override
    @Transactional
    public Boolean updateUser(UserVO user) {

        return Boolean.TRUE;
    }

    @Override
    @Transactional
    public Boolean deleteUser(Long userId, List<Long> userIds) {

        return Boolean.TRUE;
    }

    private UserVO addUserToDb(UserVO userVo) {
        /*QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq(User.COL_LOGINNAME, userVo.getLoginname());
        userQueryWrapper.last("limit 1");
        User userTemp = userDao.selectOne(userQueryWrapper);
        if (userTemp != null) {
            ExceptionCast.cast(CommonCode.USERNAME_EXSIT);
        }

        User user = new User();
        String encodedPwd = BCryptUtil.encode(userVo.getLoginpw());
        userVo.setLoginpw(encodedPwd);

        BeanUtil.copyProperties(userVo, user);
        userDao.insert(user);
        userVo.setUserid(user.getUserid());//辅助业务间调用的id返回*/

        return new UserVO();
    }
}
